package com.gitee.wsl.mathematics.matrix

import com.gitee.wsl.io.dataproxy.DataGet
import com.gitee.wsl.io.dataproxy.DataProxy
import com.gitee.wsl.mathematics.vector.MutableVector
import com.gitee.wsl.mathematics.vector.Vector


open class VectorDataGet<T:Number>(open val rows: List<Vector<T, *>>,
                                   val colCount:Int,
                                   override val size: Int = rows.size * colCount
): DataGet<T> {

    override fun get(index: Int): T {
        val (r,c) = mapIndex(size,index)
        return rows[r][c]
    }

    override fun values(): List<T> = rows.map { it.proxy.values() }.flatten()

    operator fun Vector<T, *>.get(index:Int) = this.proxy[index]

}

private fun mapIndex(colCounts:Int,index: Int):Pair<Int,Int>{
    val colIndex = index%colCounts
    val rowIndex = index/colCounts
    return rowIndex to colIndex
}

open class VectorDataProxy<T:Number>(override val rows: List<MutableVector<T, *>>,
                                     colCount:Int): VectorDataGet<T>(rows,colCount), DataProxy<T> {

    //override val size: Int = rows.size * colCount
    override fun fill(value: T, fromIndex: Int, toIndex: Int) {
        TODO("Not yet implemented")
    }

    override fun set(index: Int, value: T) {
        val (r,c) = mapIndex(size,index)
        rows[r][c] = value
    }

    operator fun MutableVector<T, *>.set(index:Int, value:T) {
        this.proxy[index] = value
    }
}